/*
 *  Copyright (C) 2010 Glauco
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.balisunrise.common.defaults;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import org.balisunrise.builder.CommonFactory;
import org.balisunrise.builder.FactoryException;
import org.balisunrise.common.EntityList;
import org.balisunrise.common.listeners.EntityListListener;
import org.balisunrise.common.events.ListChangedEvent;
import org.balisunrise.common.abstracts.AbstractBasic;

/**
 *
 * @author Glauco
 */
public class DefaultEntityList extends AbstractBasic implements EntityList{

    private List<Serializable> entityList;
    private List<EntityListListener> listeners;

    public DefaultEntityList() {
        entityList = new ArrayList<Serializable>();
        listeners = new ArrayList<EntityListListener>();
    }

    public List<Serializable> getEntityList() {
        return entityList;
    }

    public void setEntityList(List<Serializable> entityList) {
        this.entityList = entityList;
    }

    public List<EntityListListener> getListeners() {
        return listeners;
    }

    public void setListeners(List<EntityListListener> listeners) {
        this.listeners = listeners;
    }

    @Override
    public void addEntity(Serializable entity) {
        getEntityList().add(entity);
        fireEntityAdded();
    }

    @Override
    public void addList(List<? extends Serializable> list) {
        getEntityList().addAll(list);
        fireEntityListChange();
    }

    @Override
    public void setList(List<? extends Serializable> list) {
        setEntityList((List<Serializable>) list);
        fireEntityListChange();
    }

    @Override
    public Serializable[] getEntities() {
        if (getEntityList() == null || getEntityList().isEmpty())
            return new Serializable[]{};

        return getEntityList().toArray( new Serializable[0]);
    }

    @Override
    public Serializable getEntityAt(int index) {
        if (index < 0)
            return null;
        if (index >= entityList.size())
            return null;
        return entityList.get(index);
    }

    @Override
    public void addEntityListListener(EntityListListener listener) {
        getListeners().add(listener);
    }

    @Override
    public EntityListListener removeEntityListener(int index) {
        try{
            return getListeners().remove(index);
        }catch(UnsupportedOperationException uex){
            System.out.println(uex.getMessage());
        }catch(IndexOutOfBoundsException iex){
            System.out.println(iex.getMessage());
        }
        return null;
    }

    @Override
    public boolean removeEntityListener(EntityListListener listener) {
        try{
            return getListeners().remove(listener);
        }catch(Exception ex){
            System.out.println(ex.getMessage());
        }
        return false;
    }

    @Override
    public EntityListListener[] getEntityListListeners() {
        if (getListeners() == null || getListeners().isEmpty())
            return new EntityListListener[]{};

        return getListeners().toArray(new EntityListListener[0]);
    }

    private void fireEntityListChange(){

        if (getEntityListListeners() == null)
                return;

        ListChangedEvent evt = createListChangedEvent(0,
                getEntityList().size() - 1);
        if(evt == null)
            return ;

        for (EntityListListener listener : getEntityListListeners()) {
            listener.entityListChanged(evt);
        }

        getOwner().actionPerformed(evt);
    }

    private void fireEntityAdded(){

        if (getEntityListListeners() == null)
                return;

        int index = getEntityList().size() - 1;
        ListChangedEvent evt = createListChangedEvent(index, index);
        if(evt == null)
            return ;

        for (EntityListListener listener : getEntityListListeners()) {
            listener.entityAdded(evt);
        }

        getOwner().actionPerformed(evt);
    }

    private ListChangedEvent createListChangedEvent(
            int initialIndex, int finalIndex){

        try {
            return CommonFactory.getFactory()
                    .createListChangedEvent(this, initialIndex, finalIndex);
        } catch (FactoryException ex) {
            // log
        }
        return null;
    }
}
